package org.web.ssm.controller.wechatController;

import com.alibaba.fastjson.JSON;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.web.ssm.dto.ResultModel;
import org.web.ssm.exception.UserException;
import org.web.ssm.model.Product;
import org.web.ssm.model.User;
import org.web.ssm.model.queryVo.BaseQuery;
import org.web.ssm.service.IUserService;

import java.util.List;

/**
 * Created by LUOLIANG on 2016/6/10.
 */
@RequestMapping("/weuser")
@RestController
@SessionAttributes("currUser")
public class WeUserController {
    private Logger logger = LogManager.getLogger(WeUserController.class);
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/createaccount", method = RequestMethod.POST)
    public String createaccount(User user, Model model) {
        logger.info("前台传来的参数：" + user.toString());

        try {
            userService.insertUser(user);
            model.addAttribute("currUser", user);
            return JSON.toJSONString(new ResultModel<User>("注册成功！", true));
        } catch (UserException e) {
            logger.error("添加用户错误：" + e.getMessage());
            return JSON.toJSONString(new ResultModel<User>(e.getMessage(), false));
        }
    }

    @RequestMapping(value = "/islogin", method = RequestMethod.POST)
    public String islogin(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        User user = userService.getUserByUsername(username, password);

        if (user != null) {
            logger.info("登陆成功");
            model.addAttribute("currUser", user);
            return JSON.toJSONString(new ResultModel<User>("登录成功！", true));
        } else {
            return JSON.toJSONString(new ResultModel<User>("用户名或密码错误！", false));
        }
    }

    @RequestMapping("/currentUser")
    public String currentUser(@ModelAttribute("currUser") User user) {

        return JSON.toJSONString(new ResultModel<User>(true, user));
    }

    @RequestMapping("/outLogin")
    public String outLogin(@ModelAttribute("currUser") User user, SessionStatus sessionStatus) {
        user.setLoginstate(false);
        try {
            userService.updateUser(user);
            sessionStatus.setComplete();
            return JSON.toJSONString(new ResultModel<User>("退出成功！", true));
        } catch (UserException e) {
            logger.error("用户退出操作失败：" + e.getMessage());
            return JSON.toJSONString(new ResultModel<User>(e.getMessage(), false));
        }
    }

    @RequestMapping("/getuserList")
    @ResponseBody
    public ResultModel<BaseQuery<User>> getUserList(int pageSize, int pageCurrent) {
        ResultModel<BaseQuery<User>> resultModel = new ResultModel<BaseQuery<User>>();
        BaseQuery<User> result = null;
        try {
            result = userService.getUserList(pageSize, pageCurrent);
            if (result != null) {
                resultModel.setModel(result);
                resultModel.setSuccess(true);
                resultModel.setMsg("查询成功");
            } else {
                resultModel.setSuccess(true);
                resultModel.setMsg("没有数据");
            }

        } catch (UserException e) {


        }
        return resultModel;
    }


}
